
function getManage() {
    Ajax({
        url: "http://localhost/Dorm%20Management/server/manageQueryAll.php",
        success(msg) {
            sessionStorage.setItem("managInfo", msg);
            msg = JSON.parse(msg);
            randerManage(msg);
        }
    })
}
getManage()
function randerManage(msg) {
    const tbody = document.querySelector("tbody");
    tbody.innerHTML = "";
    msg.forEach((item, index) => {
        let str = `
        <tr>
        <td>${item.name}</td>
        <td>${item.power}</td>
        <td><i class="power1">${item.power1}</i></td>
        <td><i class="power2">${item.power2}</i></td>
        <td><i class="power3">${item.power3}</i></td>
        <td>
          <button type="button" class="btn btn-warning btn-sm" style="padding:5px 15px;" data-bs-toggle="modal"
            data-bs-target="#myModal2" id="${item.mid}" data-upd="upd">修改权限等级</button>
          <button type="button" id="${item.mid}" data-del="del" class="btn btn-danger btn-sm" style="padding:5px 15px;">删除</button>
        </td>
      </tr>
      
        `
        tbody.innerHTML += str;
    })
}
// 添加管理员
function addManage(){
    let login = sessionStorage.getItem('login')
    if(login == '3'){
    const username = document.querySelector("#addusername").value;
    const password = document.querySelector("#addpassword").value;
    const gender   = document.querySelector("#addgender").value;
    const addres   = document.querySelector("#addres").value;
    const name  = document.querySelector("#addname").value;
    const power = document.querySelector("#addpower").value;
    let power1 = "";
    let power2 = "";
    let power3 = "";
    console.log(power);
    if(power == '3'){
        power1 = "有权限";
        power2 = "有权限";
        power3 = "有权限"; 
    }
    if(power == '2'){
        power1 = "有权限";
        power2 = "有权限";
        power3 = "无权限";
    }
    if(power == '1'){
        power1 = "有权限";
        power2 = "无权限";
        power3 = "无权限";
    }
    Ajax({
        url:"http://localhost/Dorm%20Management/server/manageQueryAdd.php",
        data:{username,password,gender,addres,name,power,power1,power2,power3},
        success(msg){
            alert(msg);
            location.reload();
        }
    })}else{
        alert("您的权限等级不足！")
    }
}
//获取要修改的管理员信息
const tbody = document.querySelector("tbody")
tbody.onclick = function(e) {
    let login = sessionStorage.getItem('login')
    
    if(e.target.dataset.del == "del"){
       
        if(login == '3'){
         let a = confirm('确认删除该管理员吗？')   
         if(!a){
             return
         }
        Ajax({
        url:"http://localhost/Dorm%20Management/server/manageQueryRemove.php",
        data:{id:e.target.id},
        success(msg){
            alert(msg);
            // location.reload();
        }
        })
    }else{
        alert("您的权限等级不足！")
    }}
    if(e.target.dataset.upd == "upd"){
        Ajax({
            url:"http://localhost/Dorm%20Management/server/manageQueryOne.php",
            data:{id:e.target.id},
            success(msg){
                msg = JSON.parse(msg)
                document.querySelector("#updid").value = msg.mid
                document.querySelector("#updusername").value = msg.username
                updpassword = document.querySelector("#updpassword").value = msg.password
                updgander = document.querySelector("#updgander").value   = msg.gender
                document.querySelector("#updres").value      = msg.addres
                document.querySelector("#updname").value     = msg.name
                document.querySelector("#updpower").value    = msg.power
            }
            })
    }

}
function updpower(){
    let a = confirm('确认修改该管理员权限吗？')   
         if(!a){
             return
         }
    let login = sessionStorage.getItem('login')
    if(login == '3'){
     const id = document.querySelector("#updid").value
     const power = document.querySelector("#updpower").value
     let power1 = "";
    let power2 = "";
    let power3 = "";
     if(power == '3'){
        power1 = "有权限";
        power2 = "有权限";
        power3 = "有权限"; 
    }
    if(power == '2'){
        power1 = "有权限";
        power2 = "有权限";
        power3 = "无权限";
    }
    if(power == '1'){
        power1 = "有权限";
        power2 = "无权限";
        power3 = "无权限";
    }
     console.log(id,power,power1,power2,power3);
     Ajax({
         url:"http://localhost/Dorm%20Management/server/manageQueryUpdpower.php",
         data:{id,power,power1,power2,power3},
         success(msg){
             alert(msg)
             location.reload()
         }
     })
    }else{
        alert("您的权限等级不足！")
    }
}

